<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;


class CaseActionController  extends Controller{


    /*
     *  添加案由
     * */
    public function case_action_add(Request $request){

        $data=$request->only('name','aid');

    //    if(array_key_exists('aid',$data)) if(!preg_match('/^[0-9]{1,10}$/',$data['aid']))  return ThrowApiExc('20003');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');

        $res = DB::table('C_case_action')->insertGetId($data);

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }

    /*
     *  修改案由
     * */
    public function case_action_update(Request $request,$aid){

        $data=$request->only('name','aid');

        if(array_key_exists('aid',$data)) if(!preg_match('/^[0-9]{1,10}$/',$data['aid']))  return ThrowApiExc('20003');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');

        $res = DB::table('C_case_action')->where('id',$aid)->update($data);

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return ThrowApiExc('40002');
        }



    }


    /*
     *  删除案由
     * */
    public function case_action_delete($aid){

        $res = DB::table('C_case_action')->where('id',$aid)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }

    /*
     *  查找案由
     * */
    public function case_action_select(Request $request){
        $data=$request->only('name');

        if(array_key_exists('name',$data)){
            $data=DB::table('C_case_action')->where('name','like','%'.$data['name'].'%')->get();
        }else{
            $res = DB::table('C_case_action')->get();
        }

        $res = DB::table('C_case_action')->get();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }


}
